//
//  RedPacketListViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/20.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "RedPacketListViewController.h"
#import "RedPacketCell.h"
#import "UserInfoModel.h"
#import "RedPacketListModel.h"
NSString *const RedPacketCellIdentifier = @"RedPacketCellIdentifier";
@class XLPagerTabStripViewController;
@interface RedPacketListViewController ()
@property(nonatomic,assign) TableViewControllerStyle style;
@property(nonatomic, assign) NSInteger status; ///红包状态 0-未使用 1-已使用 10-已过期 （默认不传为0）
@property(nonatomic, strong) RedPacketListModel * listModel;
@end

@implementation RedPacketListViewController
- (instancetype)initWithStyle:(TableViewControllerStyle)style{
    self = [super init];
    if (self) {
         self.style = style;
        if (TableViewControllerStyle_noUsed ==  self.style) {
            self.status = 0;
        }else if (TableViewControllerStyle_used ==  self.style){
            self.status = 1;
        }else if (TableViewControllerStyle_overdue ==  self.style){
            self.status = 10;
        }
      
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
}
- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44 );
    return tableViewFrame;
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RedPacketCell class]) bundle:nil] forCellReuseIdentifier:RedPacketCellIdentifier];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)titleForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    NSString * tabTitle = @"";
    switch (self.style) {
        case TableViewControllerStyle_noUsed:
            tabTitle = @"未使用";
            break;
        case TableViewControllerStyle_used:
            tabTitle = @"已使用";
            break;
        case TableViewControllerStyle_overdue:
            tabTitle = @"已过期";
            break;
        default:
            break;
    }
    return   tabTitle;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSInteger sections = [self.listModel.list count];
    
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   NSInteger rows = self.listModel.list >0 ?1 :0;
    return rows;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
   RedPacketCell * tempCell = [tableView dequeueReusableCellWithIdentifier:RedPacketCellIdentifier];
    RedPacketCellStyle  style = RedPacketCellStyle_normal;
    RedPacketModel * model = self.listModel.list[indexPath.section];
    if ([model.status integerValue]  == 0) {
        style = RedPacketCellStyle_noUsed;
      
    }else if ([model.status integerValue]  == 1){
        style = RedPacketCellStyle_used;
    }else if ([model.status integerValue]  == 10){
        style = RedPacketCellStyle_overdue;
    }
    [tempCell setupData:model withStyle:style];
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}

- (void)getNormalTableViewNetworkData{
    self.currentPageNo = 0;
    self.pageCount = 10;
    self.listModel = nil;
    [self requestRedpackList];
}
- (void)requestRedpackList{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"User/getRedpackList" params:@{@"token":UserInfo.userToken,@"status":@(self.status),@"p":@(self.currentPageNo),@"psize":@(self.pageCount)} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!strongSelf.listModel) {
            strongSelf.listModel = [[RedPacketListModel alloc]initWithDictionary:@{@"list":responseObject[@"data"]} error:nil];
            strongSelf.currentPageNo ++;
        }else{
            if ( [responseObject[@"data"] count] > 0) {
                [strongSelf.listModel.list addObjectsFromArray:  responseObject[@"data"]];
                strongSelf.currentPageNo ++;
            }
        }
        [strongSelf updateTableView];
        NSLog(@"%@===",responseObject);
    }];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestRedpackList];
}
- (NSInteger )getNetworkTableViewDataCount{
    
    return [self.listModel.list count];
}
 
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
